import os

def merge_ts(input_dir, output_file, max_files):
    with open(output_file, 'wb') as merged_file:
        for i in range(max_files):
            filename = f"po_temp_{i}.ts"
            file_path = os.path.join(input_dir, filename)
            if not os.path.exists(file_path):
                print(f"文件不存在: {filename}")
                continue
            with open(file_path, 'rb') as source_file:
                merged_file.write(source_file.read())
    # 批量删除.ts文件
    ts_files = [f for f in os.listdir(input_dir) if f.endswith('.ts')]
    for ts_file in ts_files:
        os.unlink(os.path.join(input_dir, ts_file))
    print("完成")


if __name__ == '__main__':
    max_files = 4483  # 在此处输入文件数目
    filename = "haha"  # 在此处输入视频的名称

    input_directory = '实验数据/video2'  # 修改为包含.ts文件的目录
    output_file = f'F:\\BaiduNetdiskDownload\\杂物\\{filename}.ts'  # 修改为合并后的文件名
    merge_ts(input_directory, output_file, max_files)
